package com.zlq.login.dao;

import com.zlq.login.entity.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;


@Repository
public class UserDao {

    //Spring的jdbc模板
    //update完成增删改
    @Resource
    private JdbcTemplate jt;

    public void save(User u) {

        String sql = "insert into myuser (name, password,status) values (?,?,0)";
        jt.update(sql, u.getName(), u.getPassword());
    }

    public Integer delete(Integer id) {
        String sql = "update myuser set status=1 where id = ?";
        int i = jt.update(sql, id);
        return i;
    }

    public void update(User u) {
        String sql = "update myuser set name=?, password=? where id=?";
        jt.update(sql, u.getName(),u.getPassword(), u.getId());
    }

    public User getById(Integer id) {
        String sql = "select * from myuser where id = ?";
        return jt.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
    }

    public Integer getTotalCount() {
        String sql = "Select count(*) from myuser";
        Integer queryForObject = jt.queryForObject(sql, Integer.class);
        return queryForObject;
    }

    public List<User> getAll() {
        String sql = "Select * from myuser";
        List<User> list = jt.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return list;
    }

    public List<User> selectByPage(int numpage, int size) {
        String sql = "select * from myuser where status=0 limit ?,?";
        List<User> query = jt.query(sql, new BeanPropertyRowMapper<User>(User.class), numpage, size);
        return query;

    }

    public User selectByName(String name) {
        String sql = "select name,password from myuser where name=?";
        User user = jt.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), name);
        return user;
    }

    public List<User> selectByLike(String str) {
        String sql = "select id,name,password from myuser where name like ? ";
        List<User> query = jt.query(sql, new BeanPropertyRowMapper<User>(User.class), str);
        return query;
    }
}
